
#include <cstdio>
#include <BMap>
#include <BFile>
#include <BSpring>
#include <BSpacer>
#include <BApplication>
#include <BSonDocument>

#include "ReadWrite_osc.h"

ReadWrite_osc::ReadWrite_osc()
{
	_author = "zym";
	_version = "1.0";
	_description = "This is BWE ReadWirte Plugin for *.osc file.";
	_document.setFormat("{\n = ;\n}\n");
}
ReadWrite_osc::~ReadWrite_osc()
{

}

void ReadWrite_osc::setOptions(const BString& options)
{
	_options = options;
	BSonDocument doc;
	if (BSonNode* root = doc.read(_options))
	{

	}
	else
	{
		_document.setFormat(options);
	}

}
const BString& ReadWrite_osc::options() const
{
	return _options;
}

const BString& ReadWrite_osc::author() const
{
	return _author;
}
const BString& ReadWrite_osc::version() const
{
	return _version;
}
const BString& ReadWrite_osc::description() const
{
	return _description;
}
const BString& ReadWrite_osc::message() const
{
	return _message;
}

BObject* ReadWrite_osc::load(const BString& fileName)
{
	BFile file(fileName);
	if (!file.open(IO_ReadText))
	{
		printf("osc error -- read failed : %s\n", fileName.cstr());
		return 0;
	}

	BString currentPath = BApplication::CurrentPath();
	BApplication::SetCurrentPath(fileName.path());
	BObject* object = _document.read(file);
	BApplication::SetCurrentPath(currentPath);

	return object;
}
bool ReadWrite_osc::load(const BString& fileName, BObject* object)
{
	if (!object)
		return false;

	BFile file(fileName);
	if (!file.open(IO_ReadText))
	{
		printf("osc error -- read failed : %s\n", fileName.cstr());
		return false;
	}

	BString currentPath = BApplication::CurrentPath();
	BApplication::SetCurrentPath(fileName.path());
	bool result = _document.read(file, object);
	BApplication::SetCurrentPath(currentPath);

	return result;
}
bool ReadWrite_osc::save(const BString& fileName, const BObject* object) const
{
	if (!object)
		return false;

	BFile file(fileName);
	if (!file.open(IO_WriteText))
	{
		printf("osc error -- write failed : %s\n", fileName.cstr());
		return false;
	}

	BString currentPath = BApplication::CurrentPath();
	BApplication::SetCurrentPath(fileName.path());
	bool result = _document.write(file, object);
	BApplication::SetCurrentPath(currentPath);

	return result;
}

BObject* ReadWrite_osc::read(const BBuffer& buffer)
{
	return _document.read(buffer);
}
bool ReadWrite_osc::read(const BBuffer& buffer, BObject* object)
{
	if (!object)
		return false;
	bool result = _document.read(buffer, object);
	return result;
}
bool ReadWrite_osc::write(BBuffer& buffer, const BObject* object) const
{
	if (!object)
		return false;
	bool result = _document.write(buffer, object);
	return result;
}

